<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/bluetooth/resources/bluetooth-test.js"></script>
<script src="/bluetooth/resources/bluetooth-fake-devices.js"></script>
<script>
'use strict';
const test_desc = 'Find all characteristics in a service.';
let device, fake_peripheral;

bluetooth_test(() => getDiscoveredHealthThermometerDevice()
    .then(_ => ({device, fake_peripheral} = _))
    // Setup a device with two measurement intervals.
    .then(() =>
      fake_peripheral.setNextGATTConnectionResponse({code: HCI_SUCCESS}))
    .then(() => device.gatt.connect())
    .then(() => fake_peripheral.addFakeService({uuid: 'health_thermometer'}))
    .then(fake_health_thermometer => Promise.all([
        fake_health_thermometer.addFakeCharacteristic({
          uuid: 'measurement_interval',
          properties: ['read', 'write', 'indicate']}),
        fake_health_thermometer.addFakeCharacteristic({
          uuid: 'measurement_interval',
          properties: ['read', 'write', 'indicate']}),
        fake_health_thermometer.addFakeCharacteristic({
          uuid: 'temperature_measurement',
          properties: ['indicate']})
    ]))
    .then(() =>
      fake_peripheral.setNextGATTDiscoveryResponse({code: HCI_SUCCESS}))
    .then(() => device.gatt.getPrimaryService('health_thermometer'))
    // Actual test starts.
    .then(service => service.getCharacteristics())
    .then(characteristics => {
      // Expect three characteristic instances.
      assert_equals(characteristics.length, 3);

      let uuid_set = new Set(characteristics.map(c => c.uuid));
      // Two of the expected characteristics are 'measurement_interval',
      // so only 2 unique UUID.
      assert_equals(uuid_set.size, 2);
      assert_true(uuid_set.has(BluetoothUUID.getCharacteristic(
        'measurement_interval')));
      assert_true(uuid_set.has(BluetoothUUID.getCharacteristic(
        'temperature_measurement')));
    }), test_desc);
</script>
